﻿using System.Threading.Tasks;
using Common.Attributes;
using DotXxlJob.Core;
using DotXxlJob.Core.Model;
using Microsoft.Extensions.Logging;
using Model.Dto;
using Newtonsoft.Json;
using Service;

namespace Job
{
    /// <summary>
    /// 测试Job处理器
    /// </summary>
    [JobHandler("DemoHandler")]
    [Component(ServiceType = typeof(IJobHandler))]
    public class DemoHandler : AbstractJobHandler
    {
        private readonly ILogger<DemoHandler> _logger;
        private readonly IUserInfoService _infoService;

        public DemoHandler(ILogger<DemoHandler> logger, IUserInfoService infoService)
        {
            _logger = logger;
            _infoService = infoService;
        }

        public override async Task<ReturnT> Execute(JobExecuteContext context)
        {
            var jobParameter = context.JobParameter;
            // var testUser = await _service.SelectUserInfo(new TestUserDto()
            // {
            //     KeyId = jobParameter
            // });
            var testUserPage = await _infoService.SelectUserInfoPage(new UserInfoPageParamDto()
            {
                PageIndex = 1,
                PageSize = 10
            });
            _logger.LogDebug("DemoHandler执行开始，参数为：{0} {1}", jobParameter, JsonConvert.SerializeObject(testUserPage));
            context.JobLogger.Log("DemoHandler执行开始，参数为：{0} {1}", jobParameter,
                JsonConvert.SerializeObject(testUserPage));
            return await Task.FromResult(ReturnT.SUCCESS);
        }
    }
}